#include <stdio.h>
#include <time.h>  // 包含 time_t 所需的头文件
#include <string.h>
#include "main.h"
//#include "pass_time.h"
// 检查会员是否过期
int is_member_expired(const Member *member) {
	time_t now = time(NULL);
	// 次卡检查剩余次数
	if(member->type == times_card && member->remaining_times <= 0) {
		return 1; // 已过期（次数用完）
	}
	
	// 检查时间是否过期
	if(member->type == month_card || member->type == year_card){
		return now > member->expire_timestamp;
	}
}



// 显示会员过期信息
void display_expire_info(const Member *member) {
	if(is_member_expired(member)) { // 1过期 0不过期
		printf("会员状态: 已过期\n");

		if(member->type == times_card) {
			printf("原因: 次卡次数已用完\n");
		} else {
			// 将时间戳转换为可读格式
			struct tm *expire_tm = localtime(&member->expire_timestamp);
			printf("原因: 已于 %d年%d月%d日过期\n", 
					expire_tm->tm_year + 1900, 
					expire_tm->tm_mon + 1, 
					expire_tm->tm_mday);
		}
	}else{
		printf("当前会员未过期\n");
                if(member->type == times_card) {
                        printf(" 次卡次数:%d\n",member->remaining_times);
		}
		
		if(member->expire_timestamp != 0){
			// 将时间戳转换为可读格式
                        struct tm *expire_tm = localtime(&member->expire_timestamp);
                        printf("有效期至： %d年%d月%d日过期\n",
                                        expire_tm->tm_year + 1900,
                                        expire_tm->tm_mon + 1,
                                        expire_tm->tm_mday);		
		}			
	}
}
// 次卡刷卡
void times_card_swipe() {
	char search_key[50];
	int found_index = -1;

	printf("请输入会员卡号/ID/手机号: ");
	scanf("%49s", search_key);
	getchar();

	// 查找会员
	for(int i = 0; i < user_count; i++) {
		if(users[i].delete_state == 1) {
			continue;
		}

		if(strcmp(search_key, users[i].card_id) == 0 ||
				strcmp(search_key, users[i].user_id) == 0 ||
				strcmp(search_key, users[i].phone) == 0) {
			found_index = i;
			break;
		}
	}

	if(found_index == -1) {
		printf("未找到会员信息！\n");
		return;
	}

	Member *p = &users[found_index];

	// 检查是否是次卡
	if(p->type != times_card) {
		printf("此会员不是次卡会员，无法刷卡！\n");
		return;
	}
	
	// 减少次数
	if(p->remaining_times > 0) {
		p->remaining_times--;
		printf("刷卡成功！剩余次数: %d\n", p->remaining_times);
		save_to_file();
		        printf("=========================\n");
        printf("\n\n");
	} else {
		printf("次数已用完，无法刷卡！\n");
		        printf("=========================\n");
        printf("\n\n");
	}
}
